/**
 * 
 */
package com.yst.metho.web.application.projectversion.views;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EventListener;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.event.ChangeListener;
import nextapp.echo2.app.event.EventListenerList;

import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.projectversion.actions.version.CreatePrjVersion;
import com.yst.metho.web.application.projectversion.actions.version.EditPrjVersionAction;
import com.yst.metho.web.application.utils.AbstractSuiviComparator;
import com.yst.metho.web.application.utils.AbstractSuiviTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;

/**
 * @author perso
 * 
 */
public class ProjectVersionTableModel extends AbstractSuiviTableModel implements IListingViewModel
{
	private class ProjectVersionComparator extends AbstractSuiviComparator
	{

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2)
		{
			ProjectVersion u1 = (ProjectVersion) o1;
			ProjectVersion u2 = (ProjectVersion) o2;
			int comparaison = 0;
			switch (getColumn())
			{
			case 0:
				comparaison = u1.getVersion().compareToIgnoreCase(u2.getVersion());
				break;
			case 1:
				if (u1.getStatus() > u2.getStatus())
				{
					comparaison = 1;
				}
				else
				{
					comparaison = 2;
				}
				break;
			default:
				break;
			}
			if (!isAscending())
			{
				comparaison = -1 * comparaison;
			}
			return comparaison;
		}

	}

	private EventListenerList listeners = new EventListenerList();

	private String[] mColumnsName =
	{ "Version", "Etat" };

	// private Collection mProjectVersionsList;

	private Project mPrj;

	public ProjectVersionTableModel(Project prj) throws NamingException, FinderException, CreateException
	{
		super();
		mPrj = prj;
		setComparator(new ProjectVersionComparator());
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreatePrjVersion(this, mPrj));
		actions.add(new EditPrjVersionAction(this, mPrj));

		return actions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			mList = Locator.getProjectVersionView().findProjectVersion(mPrj.getId());
			fireTableDataChanged();
			fireDataChange();
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}

	public void addChangeListener(ChangeListener listener)
	{
		listeners.addListener(ChangeListener.class, listener);
	}

	public void removeChangeListener(ChangeListener listener)
	{
		listeners.removeListener(ChangeListener.class, listener);
	}

	protected void fireDataChange()
	{
		EventListener[] listen = listeners.getListeners(ChangeListener.class);
		ChangeEvent e = new ChangeEvent(this);
		for (int i = 0; i < listen.length; i++)
		{
			((ChangeListener) listen[i]).stateChanged(e);
		}
	}

	/**
	 * @return the projectVersionsList
	 */
	public Collection getProjectVersionsList()
	{
		return mList;
	}
}
